package com.extension.company.common.utils;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.extension.company.MyApplication;

/**
 * 作者：zwk
 * 时间：on 2019/3/1 10:09
 * 功能：百度地图定位
 */
public class BDMapUtils {

	private CityLocationListener listener;
	private LocationClient mLocationClient;
	private MyLocationListener myListener = new MyLocationListener();

	//使用静态内部类单例模式
	public static BDMapUtils getInstance() {
		return holder.instance;
	}

	private static class holder {
		private final static BDMapUtils instance = new BDMapUtils();
	}

	/**
	 * 百度地图注册
	 */
	public void onCreate() {
		mLocationClient = new LocationClient(MyApplication.getContext());
		mLocationClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setCoorType("bd09ll");
		option.setIsNeedAddress(true); // 开启获取地址名称
		option.setScanSpan(10000);
		option.setOpenGps(true);
		option.setLocationNotify(true);
		option.setIgnoreKillProcess(true);
		option.SetIgnoreCacheException(false);
		option.setWifiCacheTimeOut(5 * 60 * 1000);
		option.setEnableSimulateGps(false);
		option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
		option.setIsNeedLocationDescribe(true);
		mLocationClient.setLocOption(option);
		mLocationClient.start();
	}

	/**
	 * 取消定位
	 */
	private void stopLocation() {
		if (null != mLocationClient && mLocationClient.isStarted()) {
			mLocationClient.stop();
		}

	}


	/**
	 * 定位地址回调
	 */
	private class MyLocationListener extends BDAbstractLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {

			String addr = location.getAddrStr();    //获取详细地址信息
			String country = location.getCountry();    //获取国家
			String province = location.getProvince();    //获取省份
			String city = location.getCity();    //获取城市
			String district = location.getDistrict();    //获取区县
			String street = location.getStreet();    //获取街道信息
			listener.cityName(city, district);

		}
	}

	/**
	 * 回调方法
	 *
	 * @param listener
	 */
	public void requestLocation(CityLocationListener listener) {
		this.listener = listener;
		onCreate();
	}

	/**
	 * 城市的回调接口
	 */
	public interface CityLocationListener {
		void cityName(String cityname, String locationName);
	}


}

